package com.cl.blog.web.client;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cl.blog.common.Result;
import com.cl.blog.entity.BContent;
import com.cl.blog.entity.BUser;
import com.cl.blog.entity.BUserFollow;
import com.cl.blog.mapper.BContentMapper;
import com.cl.blog.po.RankPo;
import com.cl.blog.service.BUserFollowService;
import com.cl.blog.service.BUserService;
import com.cl.blog.service.impl.RankingSerivce;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.lettuce.core.ScoredValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author 付
 * @Date 2024-04-02 18:11
 * @Version 1.0
 * @description
 * @since 1.0
 */
@RestController
@RequestMapping("/rank")
public class RankController {

	@Autowired
	RankingSerivce rankingSerivce;
	@Autowired
	BUserService bUserService;
	@Autowired
	BUserFollowService bUserFollowService;
	@Autowired
	BContentMapper bContentMapper;


	/**
	 * 首页排行榜数据获取
	 */
	@GetMapping("/list")
	public Result getUserRanking(HttpServletRequest request) {
		Object ranks = rankingSerivce.getRankingWithHome(request);

		return Result.success(ranks);
	}

	/**
	 * 获取用户的排名信息
	 *
	 * @param userId
	 * @return
	 */
	@GetMapping("/one")
	public Result getUserRank(Integer userId) {
		Object userRanking = rankingSerivce.getUserRanking(userId);
		return Result.success(userRanking);
	}

	/**
	 * 向redis存储用户排名信息
	 *
	 * @param userId
	 * @return
	 */
	@PostMapping("/add")
	public Result insetUserRank(Integer userId) {
		rankingSerivce.addUser(userId);
		return Result.success();
	}

	@GetMapping("/contentRank")
	public Result getContentRank() {
		Object list = rankingSerivce.getContent();
		return Result.success(list);
	}

	/**
	 * 获取总榜
	 * 根据起始位置和终止位置获取排行榜数据，用于懒加载数据
	 * @param start
	 * @param end
	 * @return
	 */
	@GetMapping("/ranks")
	public Result rankListByRank(Integer start,Integer end){
		Object obj = rankingSerivce.lazyByRank(start,end);
		return Result.success(obj);
	}

	/**
	 * 获取周榜
	 * 根据起始位置和终止位置获取排行榜数据，用于懒加载数据
	 * @param start
	 * @param end
	 * @return
	 */
	@GetMapping("/week-ranks")
	public Result rankListByWeek(Integer start,Integer end){
		Object obj = rankingSerivce.lazyByWeekRank(start,end);
		return Result.success(obj);
	}


}
